import * as constants from './constants'
//List只能把数组的外层转换为Immutable数组,内层还是普通的对象，而fromJs则能全部转换为immutable类型；
import {fromJS,List} from "immutable";
import axios from "axios";
export const mouseEnter=()=>({
    type:constants.MOUSE_ENTER
});
export const mouseLeave=()=>({
    type:constants.MOUSE_LEAVE
});
export const handleMouseEnter=()=>({
    type:constants.HANDLE_MOUSE_ENTER
});
export const handleMouseLeave=()=>({
    type:constants.HANDLE_MOUSE_LEAVE
});
export const bindHandleScroll=(state)=>({
    type:constants.BIND_HANDLE_SCROLL,
    state
});
const changeHomeData=(result)=>({
     type:constants.CHANGE_HOME_DATA,
     topicList:result.topicList,
     recommendList:result.recommendList,
     articleList:result.articleList,
});

export const getHomeInfo=()=>{
    return (dispatch)=>{
        axios.get('/api/homeList.json').then((res)=>{
            // const result =res.data.data;
            dispatch(changeHomeData(res.data.data));
        }).catch((err)=>{
            console.log(err);
        })
    }
};
export const WriterInfoSwitch=(page)=>({
    type:constants.WRITER_INFO_SWITCH,
    page
});
export const getWriterList = () => {
    return (dispatch) => {
        axios.get('/api/writerList.json').then((res) => {
            const data = res.data.data.writerList;
            dispatch(changeList(data));
        }).catch((err) => {
            console.log(err);
        })
    }
};
const changeList = (data) => ({
    type: constants.CHANGE_WRITER_LIST,
    data: fromJS(data),
    totalPage: Math.ceil(data.length / 5)
});

const addArticleList=(list,nextPage)=>({
    type:constants.ADD_ARTICLE_LIST,
    list:fromJS(list),
    nextPage
});
export const getMoreArticleList=(page)=>{
    return (dispatch)=>{
       axios.get('/api/articleList.json?page'+page).then((res)=>{
           const result =res.data.data.articleList;
           console.log(result);
           dispatch(addArticleList(result,page+1));
       }).catch((err)=>{
           console.log(err);
       })
    }
};

